<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\v1\service;

use api\v1\model\CmfUserModel;
use api\v1\model\XmglGdModel;
use GdImage;
use think\facade\Db;
use think\db\Query;

class GdService
{
    /**
     * 工单分页查询
     * @param $filter 查询过滤条件
     * @param $param  查询分页、排序等参数
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getGdList($filter, $param)
    {
        $field = 'a.*,u.user_nickname';
        $filedArr = [
            'a.*',
            'u.user_nickname as user_xm',
            'jsr.user_nickname as jsr_xm',
            'gdtype.name as gdtype_mc',
            'hylx.name as hylx_mc',
            'gzlx.name as gzlx_mc',
            'zzjg.zzjg_mc as zzjg_mc',
        ];

        $m = new XmglGdModel();
        $q = $m->alias('a');
        $q->leftJoin('user u', 'a.user_id = u.id');
        $q->leftJoin('user jsr', 'a.jsr_id = jsr.id');
        $q->leftJoin('xmgl_config gdtype', 'a.gdtype_id = gdtype.id');
        $q->leftJoin('xmgl_config hylx', 'a.hylx_id = hylx.id');
        $q->leftJoin('xmgl_config gzlx', 'a.gzlx_id = gzlx.id');
        $q->leftJoin('xmgl_zzjg zzjg', 'a.zzjg_id = zzjg.id');

        //$limit       = empty($param['limit']) ? 10 : $param['limit'];
        $pageNumber = empty($param['pageNumber']) ? 1 : $param['pageNumber'];
        $pageSize = empty($param['pageSize']) ? 20 : $param['pageSize'];
        $order = empty($param['order']) ? '' : $param['order'];

        $list = $q->field(implode(",", $filedArr))
            ->where('a.id', 'IN', function (Query $q1) use ($filter, $param) {
                //使用子查询，提高查询效率
                $q1->name('xmgl_gd')->where(function (Query $q2) use ($filter, $param) {
                    //$q2->where('create_time', '>=', 0);
                    $q2->where('delete_time', 0);
    
                    //根据项目id查询
                    $xm_id = empty($filter['xm_id']) ? 0 : $filter['xm_id'];
                    if (!empty($xm_id)) {
                        $q2->where(['xm_id', '=', $xm_id]);
                    }
                    //只查询处理完成的工单，即工单已经结束
                    $end = empty($filter['end']) ? 0 : $filter['end'];
                    if (!empty($end)) {
                        $q2->where('gdjs_time > 0');
                    }
                    //只查询处理中的工单，即未完结工单
                    $processing = empty($filter['processing']) ? 0 : $filter['processing'];
                    if (!empty($processing)) {
                        $q2->where('gdjs_time = 0');   //
                    }
                    //经手人查询
                    $jsr_id = empty($filter['jsr_id']) ? 0 : $filter['jsr_id'];
                    if (!empty($jsr_id)) {
                        $q2->where("jsr_id = $jsr_id OR find_in_set($jsr_id, gcss_id)");
                    }
                    //项目组成员查询
                    $gcs_id = empty($filter['gcs_id']) ? 0 : $filter['gcs_id'];
                    if (!empty($gcs_id)) {
                        $q2->where("find_in_set($gcs_id, gcss_id)");
                    }
                    //报修开始日期
                    $ksrq = empty($filter['ksrq']) ? 0 : strtotime($filter['ksrq']);
                    if (!empty($ksrq)) {
                        $q2->where('bx_time', '>=', $ksrq);
                    }
                    //报修结束日期
                    $jsrq   = empty($filter['jsrq']) ? 0 : strtotime($filter['jsrq']);
                    if (!empty($jsrq)) {
                        $q2->where('yy_time', '<=', $jsrq);
                    }
                    //故障内容搜索
                    $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
                    if (!empty($keyword)) {
                        $q2->where('name', 'like', "%$keyword%");
                    }
                    //客户名称搜索
                    $khmc = empty($filter['khmc']) ? '' : $filter['khmc'];
                    if (!empty($khmc)) {
                        $q2->where('khmc', 'like', "%$khmc%");
                    }
                })->field('id');
            })
            ->order($order)
            ->paginate([
                'list_rows' => $pageSize,
                'page' => $pageNumber,
            ]);

        dump($q->getLastSql());

        $rowTotal = $list->total();

        $j = [
            'pageSize'          => $pageSize,
            'pageNumber'        => $pageNumber,
            "totalPage"         => ceil($rowTotal / $pageSize),
            'total'             => $rowTotal,
            'rows'              => $list,
            'msg'               => ''
        ];

        return $list;
    }

    //已废弃 ，更换成使用子查询方式提升查询效率
    public function getGdList1($filter, $param)
    {
        $field = 'a.*,u.user_nickname';
        $filedArr = [
            'a.*',
            'u.user_nickname as user_xm',
            'jsr.user_nickname as jsr_xm',
            'gdtype.name as gdtype_mc',
            'hylx.name as hylx_mc',
            'gzlx.name as gzlx_mc',
            'zzjg.zzjg_mc as zzjg_mc',
        ];

        $m = new XmglGdModel();
        $q = $m->alias('a');
        $q->join('user u', 'a.user_id = u.id');
        $q->join('user jsr', 'a.jsr_id = jsr.id');
        $q->join('xmgl_config gdtype', 'a.gdtype_id = gdtype.id');
        $q->join('xmgl_config hylx', 'a.hylx_id = hylx.id');
        $q->join('xmgl_config gzlx', 'a.gzlx_id = gzlx.id');
        $q->join('xmgl_zzjg zzjg', 'a.zzjg_id = zzjg.id');

        //$limit       = empty($param['limit']) ? 10 : $param['limit'];
        $pageNumber = empty($param['pageNumber']) ? 1 : $param['pageNumber'];
        $pageSize = empty($param['pageSize']) ? 20 : $param['pageSize'];
        $order = empty($param['order']) ? '' : $param['order'];

        $list = $q->field(implode(",", $filedArr))
            ->where('a.create_time', '>=', 0)
            ->where('a.delete_time', 0)
            ->where(function (Query $query) use ($filter) {

                //根据项目id查询
                $xm_id = empty($filter['xm_id']) ? 0 : $filter['xm_id'];
                if (!empty($xm_id)) {
                    $query->where(['xm_id', '=', $xm_id]);
                }
                //只查询处理完成的工单，即工单已经结束
                $end = empty($filter['end']) ? 0 : $filter['end'];
                if (!empty($end)) {
                    $query->where('gdjs_time>0');
                }
                //只查询处理中的工单，即未完结工单
                $processing = empty($filter['processing']) ? 0 : $filter['processing'];
                if (!empty($processing)) {
                    $query->where('gdjs_time = 0');   //
                }
                //经手人查询
                $jsr_id = empty($filter['jsr_id']) ? 0 : $filter['jsr_id'];
                if (!empty($jsr_id)) {
                    $query->where("a.jsr_id = $jsr_id OR find_in_set($jsr_id, a.gcss_id)");
                }
                //项目组成员查询
                $gcs_id = empty($filter['gcs_id']) ? 0 : $filter['gcs_id'];
                if (!empty($gcs_id)) {
                    $query->where("find_in_set($gcs_id, a.gcss_id)");
                }
                //报修开始日期
                $ksrq = empty($filter['ksrq']) ? 0 : strtotime($filter['ksrq']);
                if (!empty($ksrq)) {
                    $query->where('a.bx_time', '>=', $ksrq);
                }
                //报修结束日期
                $jsrq   = empty($filter['jsrq']) ? 0 : strtotime($filter['jsrq']);
                if (!empty($jsrq)) {
                    $query->where('a.yy_time', '<=', $jsrq);
                }
                //故障内容搜索
                $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
                if (!empty($keyword)) {
                    $query->where('a.name', 'like', "%$keyword%");
                }
                //客户名称搜索
                $khmc = empty($filter['khmc']) ? '' : $filter['khmc'];
                if (!empty($khmc)) {
                    $query->where('a.khmc', 'like', "%$khmc%");
                }
            })
            ->order($order)
            ->paginate([
                'list_rows' => $pageSize,
                'page' => $pageNumber,
            ]);

        //dump($q->getLastSql());

        $rowTotal = $list->total();

        $j = [
            'pageSize'          => $pageSize,
            'pageNumber'        => $pageNumber,
            "totalPage"         => ceil($rowTotal / $pageSize),
            'total'             => $rowTotal,
            'rows'              => $list,
            'msg'               => ''
        ];

        return $list;
    }
}
